package com.mew.chess.core.game.shbz.game;

import com.mew.chess.core.game.shbz.game.common.FishConstantValue;
import com.mew.chess.core.game.shbz.game.common.Route;
import com.mew.common.config.FishRedisValue;
import com.mew.common.config.SpringContextBean;
import com.mew.common.dc.redis.FishingRedisService;
import com.mew.common.entity.config.FishingFish;
import com.mew.common.util.NumberUtil;
import com.zebra.common.concurrent.ZebraExecutor;
import com.zebra.common.concurrent.ZebraExecutorGroup;
import com.zebra.common.concurrent.ZebraExecutorManager;
import com.zebra.common.concurrent.ZebraScheduler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Data
@Slf4j
public class ShbzGameManager {
    private int gameId;
    private static ShbzGameManager shbzGameManager = new ShbzGameManager();
    private Map<Integer, FishingFish> fish;
    private List<Route> fishRoute;
    private List<Route> fishBossRoute;

    private final Map<Integer, ShbzRoomManager> controllerMap = new HashMap<>(8);

    private ZebraExecutorGroup zebraExecutorGroup;

    private ZebraScheduler zebraScheduler;

    private ShbzGameManager(){
    }

    public static ShbzGameManager getInstance() {
        if (shbzGameManager == null) {
            synchronized (ShbzGameManager.class) {
                if (shbzGameManager == null) {
                    shbzGameManager = new ShbzGameManager();
                }
            }
        }
        return shbzGameManager;
    }

    public void putController(ShbzRoomManager controller){
        controllerMap.put(controller.getRoomId(), controller);
    }

    public ShbzRoomManager getController(int roomId){
        return controllerMap.get(roomId);
    }

    public void initallZebraExecutorGroup(){
        int size = Runtime.getRuntime().availableProcessors()*8;
        zebraExecutorGroup = new ZebraExecutorGroup(size, "Shbz-exe");
        zebraExecutorGroup.start();

        init();
        zebraScheduler = null == zebraScheduler ? ZebraExecutorManager.getInstance().getDefaultScheduler() : zebraScheduler;

        zebraScheduler.scheduleWithFixedDelay(this::init, 600L, 5*60000,TimeUnit.MILLISECONDS, ZebraExecutorManager.getInstance().getDefaultExecutor());
    }

    public void init(){
        FishingRedisService fishingRedisService = SpringContextBean.getBean(FishingRedisService.class);
        putFishs(fishingRedisService.findFish());
        FishRedisValue fishRedisValue = fishingRedisService.fishCongfig();
        FishConstantValue.fishRedisValue = fishRedisValue;
        //log.info("捕魚讀取配置------------------------------------------");
        //log.info("{}",fishRedisValue);
    }

    public ZebraExecutor getDisruptorByUserId(long userId) {
        int index = (int)(userId % (long)this.zebraExecutorGroup.size());
        return this.zebraExecutorGroup.getByIndex(index);
    }

    public void putFishs(List<FishingFish> list){
        fish = null == fish ? new HashMap<>(NumberUtil.mapSize(list.size())) : fish;
        list.forEach(f -> fish.put(f.getId(), f));
    }

    public Collection<FishingFish> findFishs(){
       return fish.values();
    }

    public FishingFish getFish(int fishId){
        return fish.get(fishId);
    }

    public Route findByType(int routeType){
        return fishRoute.get(routeType);
    }

}
